package com.ld.poetry.utils;

import com.ld.poetry.handle.PoetryRuntimeException;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class DateTimeUtil {

    public static String getTimeNow(){
        return new SimpleDateFormat(CommonConst.TIME_FORMAT_BASE).format(new Date());
    }

    public static String getTimeStrByLong(long timeLong){
        Date date = new Date(timeLong);

        return new SimpleDateFormat(CommonConst.TIME_FORMAT_BASE).format(date);
    }

    public static String getTimeAfterOneHour(){

        Calendar instance = Calendar.getInstance();

        instance.add(Calendar.HOUR,1);

        return new SimpleDateFormat(CommonConst.TIME_FORMAT_BASE).format(instance.getTime());
    }

    public static String getTimeBeforeOneHour(){

        Calendar instance = Calendar.getInstance();

        instance.add(Calendar.HOUR,-1);

        return new SimpleDateFormat(CommonConst.TIME_FORMAT_BASE).format(instance.getTime());
    }

    public static String getTimeNowOnlyDay(){
        return new SimpleDateFormat(CommonConst.TIME_FORMAT_DAY).format(new Date());
    }

    public static String getTimeNowOnlyYear(){
        return new SimpleDateFormat(CommonConst.TIME_FORMAT_YEAR).format(new Date());
    }

    public static Date getDateByStr(String str){
        try {
            return new SimpleDateFormat(CommonConst.TIME_FORMAT_BASE).parse(str);
        } catch (ParseException e) {
            log.error("时间转换错误！",e);
            throw new PoetryRuntimeException("时间转换错误！");
        }
    }

    public static long getLongByStr(String expireTime) {
        try{
            Date parse = new SimpleDateFormat(CommonConst.TIME_FORMAT_BASE).parse(expireTime);

            return parse.getTime();
        }catch (Exception e){
            throw new PoetryRuntimeException("时间转换错误!字符串:" + expireTime);
        }
    }
}
